< previous page page_369 next page >

Page 369
Private Type Struct2
   a As Integer
   reserved as Integer
   b As Long
   c As String * 4
   d As String
End Type
This structure will appear identically in memory under both single-byte and natural-byte alignment (feel free to modify the Struct.vbp project and try it).
The bad news is that, in a number of cases, Microsoft blew it and the structures do not follow the natural alignment rules. There are several ways to deal with these situations. You can redefine the Visual Basic type to deal with the alignment issuesfor example, dividing a long variable into two integers. You can temporarily copy the structure into a memory buffer such as a byte array, explicitly copying parts of the structure into the locations where they need to be. You can use a third-party product such as Desaware's SpyWorks, which includes byte-packing functions that automatically perform single-byte packing and unpacking for you.
This concludes our introduction to handling user-defined types. You'll find that many of the puzzles in this book deal with applications relating to this subject, including dealing with pointers to strings and other user-defined types, alignment issues, and working with arrays of structures.

 
< previous page page_369 next page >